<?php
  $objXajax->registerFunction("fmfrmroom_save");
  $objXajax->registerFunction("fmfrmroom_edit");
  $objXajax->registerFunction("fmfrmroom_delete");
  $objXajax->registerFunction("fmfrmroom_loadGrid");
  $objXajax->registerFunction("fmfrmroom_fillRoomType");  
  function fmfrmroom_save($params){
    global $objDbUpdate,$hotel_id;
    $objResponse = new xajaxResponse();
    $room_type_id = $params['cbbroomtype'];
    $strname = addslashes($params['name']) ;
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql ='';
    
    $objDbUpdate->Execute($update);
    if($edit==1){
     $sql = "update fm_room set name= '$strname',room_type = '$room_type_id' where id=$id and service_id = '$hotel_id' ";     
    }else{
      $strValue = fmfrmroom_createValue($strname,$room_type_id,$hotel_id);
      $sql = "insert into fm_room(name,room_type,service_id) values$strValue";  
    }
    
    if($objDbUpdate->Execute($sql)){
       $objResponse->addScript("checkUpdateStatus(1,'fmfrmroom','$edit');");
       $objResponse->addScript('xajax_fmfrmroom_loadGrid();'); 
        }else{
       $objResponse->addScript("checkUpdateStatus(0,'fmfrmroom','$edit');");
    }        
    $objResponse->addScript("hasChange= true;");
/*    $flag = $objDbUpdate->Execute($sql);
    $objResponse->addScript('xajax_fmfrmroom_loadGrid();');  */
    return $objResponse->getXML(); 
  }
  
  function fmfrmroom_createValue($strname,$room_type_id,$hotel_id){
    $strname = addslashes($strname);
      $result = "('$strname',$room_type_id,$hotel_id)"; 
      return $result;
  }
  
   function fmfrmroom_loadGrid(){
    global $lang;
    $objResponse = new xajaxResponse();
    $arrGrid = fmfrmroom_GetGrid();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/customer/fm_room_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('fmfrmroom_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('fmfrmroom_tbl');"); 
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
  }
  
    function  fmfrmroom_GetGrid(){
        global $objDbSelect,$hotel_id;
        $sqlSelect = "select r.id as id,r.active, r.name as name,rt.name roomtype from fm_room r join 
        fm_room_type rt on r.room_type = rt.id and rt.service_id =$hotel_id where r.service_id = '$hotel_id'  order by rt.name,r.name";
        return $objDbSelect->GetArray($sqlSelect);
    }
    function fmfrmroom_delete($strId){
        global $objDbSelect,$hotel_id;
       $objResponse = new xajaxResponse();
       $sql = " delete from fm_room where id in ($strId) and service_id= '$hotel_id'" ;
       $objDbSelect->Execute($sql);
       $objResponse->addScript('changeWaiting(1);');
       $objResponse->addScript('deleteRowlogic();');
       return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
    }
    
     function fmfrmroom_edit($id){
        global $objDbSelect,$hotel_id;
        $objResponse = new xajaxResponse();
        $sql = " SELECT * FROM fm_room WHERE id = '$id' and service_id= '$hotel_id' ";
        $arr = $objDbSelect->GetArray($sql);
        
        $roomtype   = $arr[0]['room_type'];
                
        $roomname =htmlspecialchars_decode($arr[0]['name']) ;
        $objResponse->addAssign('fmfrmroom_name','value',$roomname);
        $objResponse->addScript("$('#fmfrmroom_cbbroomtype').val($roomtype);");
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
   
    function fmfrmroom_fillRoomType(){
        global $objDbSelect,$hotel_id;
        $objResponse = new xajaxResponse();
        $sql = "SELECT  * FROM fm_room_type  Where service_id = $hotel_id";
        
        $arr = $objDbSelect->GetArray($sql);
       
        $count = count($arr);
        for($i=0;$i<$count;$i++){
            $id = $arr[$i]['id'];
            $name = $arr[$i]['name'];
            $temp.= "<option value='$id'> $name </option>"  ;
        }
        $temp =  '<select id="fmfrmroom_cbbroomtype" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
        $objResponse->addAssign('fmfrmroom_tdroomtype', 'innerHTML',$temp);
        return $objResponse->getXML(); // dong luon luon co khi goi ham ajax  
    }
?>
